
package com.example.paper.common.datam.migrate.writer;

import com.example.paper.common.datam.constant.KeyConstant;
import com.example.paper.common.datam.migrate.job.JobCache;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * 描述.
 *
 * @author Long Fengfan, 2022年10月17日

 */
@Component
public abstract class BaseWriter<T> implements Writer<T> {


    @Override
    public void write(Map<String, Object> processParams, List<T> infos) {
        String jobId = processParams.get(KeyConstant.job_id).toString();
        Integer processId = (Integer) processParams.get(KeyConstant.process_id);
//        一次判断
        boolean isError = JobCache.isErrorCache.get(jobId).get(processId);
        if(CollectionUtils.isEmpty(infos) || !isError) {
            doWrite(infos);
//            二次判断
            isError = JobCache.isErrorCache.get(jobId).get(processId);
            if(isError) {
                rollBack(processParams);
            }
        }
    }

    public abstract void doWrite(List<T> infos);
}
